package cn.dyw.cloudmusic.config;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * @author: dyw770
 * @Date: 2018/12/19 21:45
 * @Description: mybatis 配置
 */
public class MybatisConfig {

    private static SqlSessionFactory sqlSessionFactory;
    private  MybatisConfig() {
    }

    static {
        try {
            //形成输入流
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory  getSqlSessionFactory(){
        return sqlSessionFactory;
    }
    public static SqlSession getSession(){
        return getSqlSessionFactory().openSession();
    }
}
